package coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// ListItemCategoryConfigurationPtrsByCouponID 获取优惠券到商品品类配置数组
func (s *CouponEntityServiceImp) ListItemCategoryConfigurationPtrsByCouponID(ctx context.Context, q *sqlc.Queries, couponID int64) (arr param.ItemCategoryConfigurationPtrArray, err error) {
	couponItemCategories, err := q.ListNormalCouponItemCategoriesByCouponID(ctx, couponID)
	if err != nil {
		return
	}
	for _, couponItemCategory := range couponItemCategories {
		arr = append(arr, &param.ItemCategoryConfiguration{
			ID:   couponItemCategory.ItemCategoryID,
			Name: couponItemCategory.ItemCategoryName,
		})
	}
	return
}

// GetMapOfCouponIDToItemCategoryConfigurationPtrs 获取优惠券到商品品类配置的映射
func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemCategoryConfigurationPtrsByCouponIDs(ctx context.Context, q *sqlc.Queries, couponIDs []int64) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	m = make(param.EntityIDToItemCategoryConfigurationPtrsMap)
	if len(couponIDs) == 0 {
		return
	}
	couponItemCategories, err := q.ListNormalCouponItemCategoriesByCouponIDs(ctx, couponIDs)
	if err != nil {
		return
	}
	for _, couponItemCategory := range couponItemCategories {
		couponID := couponItemCategory.CouponID
		valPtr := &param.ItemCategoryConfiguration{
			ID:   couponItemCategory.ItemCategoryID,
			Name: couponItemCategory.ItemCategoryName,
		}
		if val, ok := m[couponID]; ok {
			val = append(val, valPtr)
			m[couponID] = val
		} else {
			m[couponID] = []*param.ItemCategoryConfiguration{valPtr}
		}
	}
	return
}

func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemCategoryConfigurationPtrsByCouponPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.Coupon) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	return s.GetMapOfCouponIDToItemCategoryConfigurationPtrsByCouponIDs(ctx, q, s.ConvToCouponIDs(ptrs))
}

func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemCategoryConfigurationPtrsByListCouponWithStatRowPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.ListCouponWithStatsRow) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	return s.GetMapOfCouponIDToItemCategoryConfigurationPtrsByCouponIDs(ctx, q, s.ConvToCouponIDs(ptrs))
}
